// fpLong.cpp: implementation of the fpLong class.
//
// 17/7/2000 MF - change from john to fix font size problem on multi
//				  question numerics (width not increased when font size
//				  increased) - log ref: 26018.
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "fpLong.h"
#include "fpLongPeer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#ifdef FPCONTROLS
#ifndef _UNICODE

IMPLEMENT_DYNAMIC(fpLong, AWTComponent)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

fpLong::fpLong()
{
	construct(CString(""), 0);
}

fpLong::fpLong(LPTSTR text)
{
	construct(CString(text), lstrlen(text));
}

fpLong::fpLong(CString& text)
{
	construct(text, text.GetLength());
}

fpLong::fpLong(int columns)
{
	construct(CString(""), columns);
}

fpLong::~fpLong()
{
	if (m_actionListener != NULL)
	{
		if (m_actionListener->isMultiCaster())
		{
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_actionListener);
			delete mc;
		}
	}
	if (m_textListener != NULL)
	{
		if (m_textListener->isMultiCaster())
		{
#ifdef UNDER_CE
			AWTEventMulticaster* mc = (AWTEventMulticaster*)m_textListener;
#else
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_textListener);
#endif
			delete mc;
		}
	}
}

CWnd* fpLong::createComponent()					 
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	fpLongPeer* pwnd = new fpLongPeer(this);
	DWORD dwStyle = WS_CHILD | WS_VISIBLE;
	pwnd->Create(dwStyle, getBounds(), wParent, 0);
	pwnd->SetFont(CFont::FromHandle(m_font));
	pwnd->SetText(m_text.GetBuffer(0));
	pwnd->SetMarginTop(0);
	pwnd->SetMarginLeft(1);
	pwnd->SetAppearance(2);
	pwnd->SetButtonStyle(1);
//	if (m_min != 0)
//	{
		TCHAR s[128];
		wsprintf(s, "%01ld", m_min);
		pwnd->SetMinValue(s);	
//	}
	if (m_max != 0)
	{
		TCHAR s[128];
		wsprintf(s, "%01ld", m_max);
		pwnd->SetMaxValue(s);	
	}
	AWTColor* c = getForeground();
	if (c != NULL)
		pwnd->SetForeColor(c->getColorRef());
	if (!m_enabled)
		pwnd->EnableWindow(FALSE);
	return pwnd;
}

CSize fpLong::preferredSize(int columns) 
{
	if (m_peer == NULL)
		return CSize(0,0);
	CSize sz(0,0);
	fpLongPeer* pwnd = (fpLongPeer*)m_peer;
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	
	// 17/7/2000 mf 
	//TEXTMETRIC tm;
	//pdc->GetTextMetrics(&tm);
	//pdc->SelectObject(ftOld);
	//pwnd->ReleaseDC(pdc);	
	//sz.cx = (tm.tmAveCharWidth * columns) + 24;
	//sz.cy = tm.tmHeight + 6;
	TCHAR c[256];
	for (int n = 0; n < columns; n++)
		c[n] = '0';
	c[columns] = '\0';
	GetTextExtentPoint32(pdc->m_hDC, c, columns, &sz);
	sz.cx += 6;		// allows for space either side
	sz.cy += 6;		// allows for space above

	sz.cx += 24; //spinner

	// numerics also require a few chars in between each digits
//	sz.cx += (columns * 10);

	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	// 17/7/2000 mf end
	return sz;
}

CSize fpLong::preferredSize() 
{
	if (m_peer == NULL)
		return CSize(0,0);
	return preferredSize(m_columns);
}

CSize fpLong::minimumSize(int columns) 
{
	if (m_peer == NULL)
		return CSize(0,0);
	CSize sz(0,0);
	fpLongPeer* pwnd = (fpLongPeer*)m_peer;
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	sz.cx = (tm.tmAveCharWidth * columns) + 24;
	sz.cy = tm.tmHeight + 6;
	return sz;
}

CSize fpLong::minimumSize() 
{
	if (m_peer == NULL)
		return CSize(0,0);
	return minimumSize(m_columns);
}

void fpLong::setColumns(int columns)
{
	if (columns < 0) return;
	if (m_columns != columns)
	{
		m_columns = columns;
		invalidate();
	}
}

void fpLong::setText(CString& text)
{
	m_text = text;
	if (m_peer != NULL)
	{	
//		m_peer->PostMessage(WM_AWTSETTEXT, 0, (LPARAM)new CString(text));
		((fpLongPeer*)m_peer)->SetText(m_text.GetBuffer(0));	
	}
}

void fpLong::setText(int n)
{
	char sz[32];
	wsprintf(sz, "%d", n);
	setText(CString(sz));
}

CString& fpLong::getText()
{
	if (m_peer != NULL)
	{
		TCHAR sz[256];
		((fpLongPeer*)m_peer)->GetText(sz, sizeof(sz));
		m_text = sz;
	}
	return m_text;
}

void fpLong::setMax(long l)
{
	m_max = l;
	if (m_peer != NULL)
	{
		TCHAR s[128];
		wsprintf(s, "%01ld", l);
		((fpLongPeer*)m_peer)->SetMinValue(s);	
	}
}

void fpLong::setMin(long l)
{
	m_min = l;
	if (m_peer != NULL)
	{
		TCHAR s[128];
		wsprintf(s, "%01ld", l);
		((fpLongPeer*)m_peer)->SetMaxValue(s);	
	}
}

void fpLong::setForeground(AWTColor* c)
{
	AWTComponent::setForeground(c);
	if (m_peer != NULL)
		((fpLongPeer*)m_peer)->SetForeColor(c->getColorRef());
}

void fpLong::processTextEvent(AWTTextEvent* e) 
{
	if (m_textListener != NULL) 
	{
		int id = e->getId();
	  switch (id) 
		{
			case AWTTextEvent::TEXT_VALUE_CHANGED:
				m_textListener->textValueChanged(e);
				break;
	  }
  }
}

#endif
#endif //FPCONTROLD
